package com.wjk.kylin.id.core.util;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * DateUtils provides date formatting, parsing
 *
 * @author wjk
 */
public abstract class DateUtils {
    /**
     * Patterns
     */
    public static final String DAY_PATTERN = "yyyy-MM-dd";
    public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

    /**
     * yyyy-MM-dd 时间转换为秒
     *
     * @param str
     * @return
     */
    public static long getEpochSecond(String str) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DAY_PATTERN);
        LocalDate localDate = LocalDate.parse(str, formatter);
        Instant instant = localDate.atStartOfDay(ZoneOffset.of("+8")).toInstant();

        return instant.getEpochSecond();
    }

    /**
     * 秒转换为时间 'yyyy-MM-dd HH:mm:ss'
     *
     * @param second
     * @return
     */
    public static String formatBySecondPattern(long second) {
        Instant instant = Instant.ofEpochSecond(second);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.of("+8"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATETIME_PATTERN);

        return localDateTime.format(formatter);
    }
}
